package cn.fl.capital.service.impl;

import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.fl.capital.condition.CapFundQueryCondition;
import cn.fl.capital.service.ICapFundDivService;
import cn.fl.capital.service.ICapFundService;
import cn.fl.capital.vo.CapFundVO;
import cn.fl.custpay.dto.DataResultDTO;
import cn.fl.framework.base.context.CurrentThreadContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 资金拆分服务
 * 作者:hongyanbo
 * 时间:2018/10/19
 */
@Service
public class CapFundDivServiceImpl  implements ICapFundDivService{
    public static final Logger LOGGER= LoggerFactory.getLogger(CapFundDivServiceImpl.class);
    @Autowired
    private ICapFundService capFundService;
    @Override
    public DataResultDTO divAndSave(Long id, Long amt) {
        DataResultDTO res = new DataResultDTO(0,"拆分失败！");
        CapFundVO capFundVO = this.capFundService.selectById(id);
        if(capFundVO!=null){
            Long availableAmount = capFundVO.getAvailableAmount();
            if(amt > availableAmount){
                res = new DataResultDTO(0,"拆分失败:已超出可用金额！");
                return res;
            }
            genChild(capFundVO,amt);
            updateP(capFundVO,amt);
            res.setSuccess(1);
            res.setCode("1");
            res.setInfo("拆分成功！");
        }else{
            BaseLogUtils.info(LOGGER ,"divAndSave","资金拆分失败，原因：查询母资金失败，母资金ID："+id);
        }
        return res;
    }

    @Override
    public DataResultDTO delDiv(Long id) {
        DataResultDTO res = new DataResultDTO(1,"删除成功");
        CapFundVO child = this.capFundService.selectById(id);
        if(child.getIsDel().equals(1)){
            res = new DataResultDTO(0,"已经被删除了");
            return res;
        }
        CapFundVO parent = this.capFundService.selectById(child.getFundPid());
        if(child.getUsedAmount() >0){
            res = new DataResultDTO(0,"不允许删除已经使用的资金");
            return res;
        }
        Long amt = child.getAvailableAmount() * -1;
        updateP(parent,amt);
        child.setIsDel(1);
        this.capFundService.modify(child);
        return res;
    }

    /**
     * 修改母资金。
     * @param capFundVO
     * @param amt
     */
    private void updateP(CapFundVO capFundVO, Long amt) {
        Long availableAmount = capFundVO.getAvailableAmount();
        Long usedAmount = capFundVO.getUsedAmount();
        availableAmount = availableAmount-amt;
        usedAmount = usedAmount + amt;
        capFundVO.setAvailableAmount(availableAmount);
        capFundVO.setUsedAmount(usedAmount);
        if(usedAmount <0){
            throw new BaseException("删除次数过多，使用金额小于0了");
        }
        this.capFundService.modify(capFundVO);
    }

    /**
     * 生成子资金对象
     * @param capFundVO
     */
    private CapFundVO genChild(CapFundVO capFundVO,Long amt) {
        CapFundVO res = new CapFundVO();
        CapFundQueryCondition qc = new CapFundQueryCondition();
        qc.setFundPid(capFundVO.getId());
        List<CapFundVO> capFundVOS = this.capFundService.searchList(qc);
        int count = 1 ;
        if(CollectionUtils.isNotEmpty(capFundVOS)){
            count = capFundVOS.size();
            count ++;
        }
        //资金编号规则
        res.setCstId(capFundVO.getCstId());
        res.setCstNm(capFundVO.getCstNm());
        res.setCode(capFundVO.getCode() +"-"+ count);
        res.setComeCode(capFundVO.getComeCode());
        res.setComeName(capFundVO.getComeName());
        res.setAcceptDate(capFundVO.getAcceptDate());
        res.setAcceptName(capFundVO.getAcceptName());
        res.setAmount(amt);
        res.setAvailableAmount(amt);
        res.setUsedAmount(0l);
        res.setFundStatusCd(capFundVO.getFundStatusCd());
        res.setAcceptDate(capFundVO.getAcceptDate());
        res.setAcceptActualDate(capFundVO.getAcceptActualDate());
        res.setDocketRem(capFundVO.getDocketRem());
        res.setFundSourcesCd(capFundVO.getFundSourcesCd());
        res.setFundInputCd(capFundVO.getFundInputCd());
        res.setFundBelongOrgCd(capFundVO.getFundBelongOrgCd());
        res.setBankCardNo(capFundVO.getBankCardNo());
        res.setFundTypCd(capFundVO.getFundTypCd());
        res.setCertificateCd(capFundVO.getCertificateCd());
        res.setCertificateCode(capFundVO.getCertificateCode());
        res.setBorrowingLogoCd(capFundVO.getBorrowingLogoCd());
        res.setMoneyType(capFundVO.getMoneyType());
        res.setExpStatusCd(capFundVO.getExpStatusCd());
        res.setRelationPayId(capFundVO.getRelationPayId());
        res.setIsDel(capFundVO.getIsDel());
        res.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
        res.setCrtTm(new Date());
        res.setVersion(capFundVO.getVersion());
        res.setFundPid(capFundVO.getId());
        res.setCapTypCd(capFundVO.getCapTypCd());
        this.capFundService.add(res);
        return res;
    }
}
